﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class SysGroup
    {
        private readonly Chain.IDAL.SysGroup dal = new Chain.IDAL.SysGroup();

        public int Add(Chain.Model.SysGroup model)
        {
            return this.dal.Add(model);
        }

        public DataTable BuildGroupTree(DataTable dt, int rootGroupID, int depth, ref DataTable dtTree)
        {
            DataRow[] rowArray = dt.Select("ParentGroupID=" + rootGroupID);
            for (int i = 0; i < rowArray.Length; i++)
            {
                string str = "|---".PadLeft((depth * 2) + 4, '　');
                rowArray[i]["GroupName"] = str + rowArray[i]["GroupName"].ToString();
                dtTree.Rows.Add(rowArray[i].ItemArray);
                this.BuildGroupTree(dt, int.Parse(rowArray[i]["GroupID"].ToString()), depth + 1, ref dtTree);
            }
            return dtTree;
        }

        public List<Chain.Model.SysGroup> DataTableToList(DataTable dt)
        {
            List<Chain.Model.SysGroup> list = new List<Chain.Model.SysGroup>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.SysGroup item = new Chain.Model.SysGroup();
                    if ((dt.Rows[i]["GroupID"] != null) && (dt.Rows[i]["GroupID"].ToString() != ""))
                    {
                        item.GroupID = int.Parse(dt.Rows[i]["GroupID"].ToString());
                    }
                    if ((dt.Rows[i]["GroupName"] != null) && (dt.Rows[i]["GroupName"].ToString() != ""))
                    {
                        item.GroupName = dt.Rows[i]["GroupName"].ToString();
                    }
                    if ((dt.Rows[i]["GroupRemark"] != null) && (dt.Rows[i]["GroupRemark"].ToString() != ""))
                    {
                        item.GroupRemark = dt.Rows[i]["GroupRemark"].ToString();
                    }
                    if ((dt.Rows[i]["ParentGroupID"] != null) && (dt.Rows[i]["ParentGroupID"].ToString() != ""))
                    {
                        item.ParentGroupID = int.Parse(dt.Rows[i]["ParentGroupID"].ToString());
                    }
                    if ((dt.Rows[i]["ParentIDStr"] != null) && (dt.Rows[i]["ParentIDStr"].ToString() != ""))
                    {
                        item.ParentIDStr = dt.Rows[i]["ParentIDStr"].ToString();
                    }
                    if ((dt.Rows[i]["IsPublic"] != null) && (dt.Rows[i]["IsPublic"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["IsPublic"].ToString() == "1") || (dt.Rows[i]["IsPublic"].ToString().ToLower() == "true"))
                        {
                            item.IsPublic = true;
                        }
                        else
                        {
                            item.IsPublic = false;
                        }
                    }
                    if ((dt.Rows[i]["CreateUserID"] != null) && (dt.Rows[i]["CreateUserID"].ToString() != ""))
                    {
                        item.CreateUserID = int.Parse(dt.Rows[i]["CreateUserID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int GroupID)
        {
            return this.dal.Delete(GroupID);
        }

        public int DeleteGroup(Chain.Model.SysGroup model)
        {
            if ((model == null) || (model.GroupID == 0))
            {
                return -1;
            }
            Chain.BLL.SysGroupAuthority authority = new Chain.BLL.SysGroupAuthority();
            this.UpdateParentId(model.GroupID, model.ParentGroupID);
            this.Delete(model.GroupID);
            authority.DeleteList(model.GroupID);
            return 1;
        }

        public bool DeleteList(string GroupIDlist)
        {
            return this.dal.DeleteList(GroupIDlist);
        }

        public bool Exists(int GroupID)
        {
            return this.dal.Exists(GroupID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetGroupAuthority(int GroupID)
        {
            return this.dal.GetGroupAuthority(GroupID);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListS(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListS(PageSize, PageIndex, strWhere, out resCount);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.SysGroup GetModel(int GroupID)
        {
            return this.dal.GetModel(GroupID);
        }

        public List<Chain.Model.SysGroup> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool Update(Chain.Model.SysGroup model)
        {
            return this.dal.Update(model);
        }

        public bool UpdateParentId(int OldParentGroupID, int NewParentGroupID)
        {
            return this.dal.UpdateParentId(OldParentGroupID, NewParentGroupID);
        }
    }
}

